package org.hxyjs.service.impl;


import org.hxyjs.dao.ShoppingtrolleyDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Shoppingtrolley;
import org.hxyjs.entity.Shoppingtrolley;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.ShoppingtrolleyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("ShoppingtrolleyService")
public class ShoppingtrolleyServiceImpl implements ShoppingtrolleyService {
    @Resource
    ShoppingtrolleyDao shoppingtrolleyDao;
    @Override
    public DTO<Shoppingtrolley> getAllShoppingtrolleys()  throws Exception{
        List<Shoppingtrolley> shoppingtrolleys = shoppingtrolleyDao.getAllShoppingtrolleys();
        MessageEnum messageEnum  = Optional.ofNullable(shoppingtrolleys).isPresent()?MessageEnum.GETSHOPPINGTROLLEYSUCCESS: MessageEnum.GETSHOPPINGTROLLEYFAIL;
        return new DTO<Shoppingtrolley>(messageEnum.getCode(),messageEnum.getMessage(),shoppingtrolleys);
    }

    @Override
    public DTO<Shoppingtrolley> deleteShoppingtrolleyByStno(String stno)  throws Exception{
        Integer result = shoppingtrolleyDao.deleteShoppingtrolleyByStno(stno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETESHOPPINGTROLLEYSUCCESS:MessageEnum.DELETESHOPPINGTROLLEYFAIL;
        return new DTO<Shoppingtrolley>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Shoppingtrolley> updateShoppingtrolleyByStno(Shoppingtrolley shoppingtrolley)  throws Exception{
        Integer result = shoppingtrolleyDao.updateShoppingtrolleyByStno(shoppingtrolley);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATESHOPPINGTROLLEYSUCCESS:MessageEnum.UPDATESHOPPINGTROLLEYFAIL;
        return new DTO<Shoppingtrolley>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Shoppingtrolley> addShoppingtrolley(Shoppingtrolley shoppingtrolley) throws Exception {
        Integer result = shoppingtrolleyDao.addShoppingtrolley(shoppingtrolley);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDSHOPPINGTROLLEYSUCCESS:MessageEnum.ADDSHOPPINGTROLLEYFAIL;
        return new DTO<Shoppingtrolley>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Shoppingtrolley> getShoppingtrolleyByPage(PageInfo pageInfo ) throws Exception {
        List<Shoppingtrolley> shoppingtrolleys = shoppingtrolleyDao.getShoppingtrolleyByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(shoppingtrolleys).isPresent()?MessageEnum.GETSHOPPINGTROLLEYSUCCESS: MessageEnum.GETSHOPPINGTROLLEYFAIL;
        return new DTO<Shoppingtrolley>(messageEnum.getCode(),messageEnum.getMessage(),shoppingtrolleys,pageInfo);
    }
}
